package com.guli.mall.pms.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.guli.common.pojo.vo.TreeNode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guli.mall.pms.entity.CategoryEntity;
import com.guli.mall.pms.service.CategoryService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;


/**
 * 商品三级分类
 *
 * @author 罗俊华
 * @email 1007052116@qq.com
 * @date 2020-09-11 20:31:22
 */
@RestController
@RequestMapping("pms/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    @GetMapping("/treeNodeList")
    public R treeNodeList() {

        TreeNode<CategoryEntity> treeNode = this.categoryService.treeNodeList();

        return R.ok().put("treeNode", treeNode);
    }

    @GetMapping("/treeCategoryList")
    public R treeCategoryList() {

        CategoryEntity treeCategoryList = this.categoryService.treeCategoryList();

        return R.ok().put("treeCategoryList", treeCategoryList);
    }

    @PostMapping("/removeByCategoryIds/")
    public R removeByCategoryIds(@RequestBody List<Long> cateGoryIds) {

        this.categoryService.removeByCategoryIds(cateGoryIds);

        return R.ok();
    }


    /**
     * 列表
     */
    @GetMapping("/list/{currentPage}/{size}")
    public R list(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size) {
        PageUtils page = categoryService.queryPage(currentPage, size);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CategoryEntity category) {
        category.setShowStatus(1);
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody CategoryEntity category) {

        List<CategoryEntity> categoryEntities = Arrays.asList(category);

        categoryService.updateBatchDetail(categoryEntities);

        return R.ok();
    }

    @PostMapping("/updateBatch")
    public R updateBatch(@RequestBody CategoryEntity[] category) {

        categoryService.updateBatchDetail(Arrays.asList(category));
        return R.ok();
    }

    @ApiOperation("批量删除")
    @PutMapping("/deleteBatch")
    public R delete(@RequestBody Long[] catIds) {

        categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

    @ApiOperation("根据categoryId查询出该category的id路径，返回从parentCategoryId到该categoryId的一个数组")
    @GetMapping("/findCategoryIdPath/{categoryId}")
    public List<Long> findCategoryIdPath(@PathVariable("categoryId") Long categoryId){

        List<Long> categoryIdPath = categoryService.findCategoryIdPath(categoryId);

        return categoryIdPath;
    }


    @GetMapping("/getCategoryName/{categoryId}")
    public String getCategoryName(@PathVariable("categoryId") Long categoryId){
        CategoryEntity byId = categoryService.getById(categoryId);

        return byId.getName();
    }

}
